package com.yeban.javase.day20.annotation.test.test3;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @Desc TODO
 * @Author yeban
 * @Date 2025/11/5
 */
public class MyClass {

    @MyTest
    public void test1() {
        System.out.println("---------- test1 ----------");
    }

    public void test2() {
        System.out.println("---------- test2 ----------");
    }

    @MyTest
    public void test3() {
        System.out.println("---------- test3 ----------");
    }

    @MyTest
    private void test4() {
        System.out.println("---------- test4 ----------");
    }

    public static void main(String[] args) {
        MyClass myClass = new MyClass();
        // 1.获取MyClass类类型
        Class<MyClass> clazz = MyClass.class;
        // 2.获取这个类的所有方法
        Method[] methods = clazz.getDeclaredMethods();
        // 迭代各方法，判断是否有MyTest方法
        for (Method method : methods) {
            method.setAccessible(true);
            if (method.isAnnotationPresent(MyTest.class)) {
                // 执行方法
                try {
                    method.invoke(myClass);
                } catch (IllegalAccessException | InvocationTargetException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
}
